package com.phoxell.kalkr;

import com.phoxell.Agent;
import com.phoxell.Serial;
import java.io.IOException;
import javax.servlet.http.*;

@SuppressWarnings("serial")
public class FirmwareServlet extends HttpServlet{
	public void doGet(HttpServletRequest req, HttpServletResponse rsp)
			throws IOException{
		try{
			Agent a=new Agent(req.getHeader("User-Agent"));
			int r=a.firmware;
			Serial i=new Serial(a.serial);
			int f=(int)i.getFlags();
			String c0="1666887";
			String j0="/kalkr/30v05.bin";
			String v0="5";
			String c1="1234567";
			String j1="/kalkr/30v04.bin";
			String v1="4";
			switch(f){
			case 2:
				c0="1666887";
				v1="6";
				break;
			default:
				if(r>3)
					throw new Exception();
				c0="1666887";
				j0="/kalkr/30v03.bin";
				v0="3";
			}
			if(r%2==0){
				rsp.addHeader("Version",v0);
				rsp.addHeader("Checksum",c0);
				System.out.println(j0);
				rsp.sendRedirect(j0);
			}else{
				rsp.addHeader("Version",v1);
				rsp.addHeader("Checksum",c1);
				System.out.println(j1);
				rsp.sendRedirect(j1);
			}
			return;
		}catch(Exception e){
			//e.printStackTrace();
		}
		rsp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
	}
}